<?php

namespace App\Http\Controllers\Plugin;

use AlibabaCloud\Client\AlibabaCloud;

use OSS\Core\OssException;
use AlibabaCloud\SDK\Sts\V20150401\Sts;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Sts\V20150401\Models\AssumeRoleRequest;
trait AliAssumeRole
{
    public static function createClient($accessKeyId, $accessKeySecret){
        $config = new Config([
            // 您的AccessKey ID
            "accessKeyId" => $accessKeyId,
            // 您的AccessKey Secret
            "accessKeySecret" => $accessKeySecret
        ]);
        // 访问的域名
        $config->endpoint = "sts.cn-shanghai.aliyuncs.com";
        return new Sts($config);
    }
    /**
     * @param string[] $args
     * @return void
     */
    public static function main(){
        $oss = config('aliyun.tst');
            $client = self::createClient($oss['accessKeyId'], $oss['accessKeySecret']);
            $assumeRoleRequest = new AssumeRoleRequest([
                "durationSeconds" => $oss['DurationSeconds'],
                "roleArn" => $oss['RoleArn'],
                "roleSessionName" => $oss['RoleSessionName']
            ]);
            // 复制代码运行请自行打印 API 的返回值
            $ret =  $client->assumeRole($assumeRoleRequest);
            return $ret->body;
    }
}
